package com.paradogs.gs.modules.world;

import com.paradogs.common._cfg.Cfg;
import com.paradogs.gs.db.World;
import com.paradogs.gs.misc.Bow;
import com.paradogs.gs.misc.DBSingleRowManager;
import com.paradogs.gs.misc.annotations.GameManager;
import com.paradogs.gs.modules.WorldObject;
import com.paradogs.gs.modules.world.base.unit.WorldPlayer;
import com.paradogs.gs.modules.world.dao.WorldDao;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;

/** By @CodeAutoGeneratorModule */
@GameManager(sync = false)
public class WorldMgr extends DBSingleRowManager<WorldDao, World> {

    @Getter
    private WorldPlayer worldObject;

    @Autowired
    private WorldService worldService;

    @Override
    public void onInitialization(World data) {
        data.setHp(Integer.MAX_VALUE);
        data.setWorldId(Cfg.RoleBase.global().initWorld());
        data.setX(Cfg.RoleBase.global().initX());
        data.setY(Cfg.RoleBase.global().initY());
        data.setHp(100);
        data.setMp(100);
    }

    @Override
    public void Start() {
        // 创建大世界对象
        worldObject = new WorldPlayer(getGameObject());
        // 初始化到出生点
        worldObject.born();
    }

    @Override
    public void Destroy() {
        super.Destroy();
        worldObject.leave(__raw__().getWorldId(), __raw__().getX(), __raw__().getY());
    }

    public void move(Integer direction) {
        getWorldObject().move(direction);
        refresh();
    }

    private void refresh() {
        World world = __get__();
        world.setWorldId(getWorldObject().getWorldId());
        world.setHp(100);
        world.setX(getWorldObject().getX());
        world.setY(getWorldObject().getY());
        __update__(world);
    }

    public void attack(Long targetId) {
        getWorldObject().attack(targetId);
    }

    public void pickup(Long targetId) {
        Bow.asserts(worldObject.canPickUp(), "无法拾取");
        WorldObject target = worldObject.curNode().findObject(targetId);
        Bow.asserts(target != null && target.canBePickUp(), "拾取目标无效");
        worldObject.pickUp(target);
    }

}
